package cn.edu.zjicm.wordsnet_d.activity.base;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.LeaderboardActivity;
import cn.edu.zjicm.wordsnet_d.activity.PersonalAccountActivity;
import cn.edu.zjicm.wordsnet_d.activity.PunchOutCalendarActivity;
import cn.edu.zjicm.wordsnet_d.activity.RegisterActivity;
import cn.edu.zjicm.wordsnet_d.activity.SettingActivity;
import cn.edu.zjicm.wordsnet_d.activity.SettingPlanActivity;
import cn.edu.zjicm.wordsnet_d.activity.WordsBookActivity;
import cn.edu.zjicm.wordsnet_d.adapter.MenuAdapter;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.data.StaticData;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.download.UpdateManager;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.view.CircleImageView;
import cn.edu.zjicm.wordsnet_d.view.ShareDialog;

import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.SyncListener;
import com.umeng.fb.model.Conversation;
import com.umeng.fb.model.Reply;

public class BaseSlideActivity extends BaseActivity {
	protected SlidingPaneLayout mSlidingPaneLayout;
	private RelativeLayout mMenu;
	private FrameLayout mBaseTitle;
	private FrameLayout mBaseContent;
	private ListView menuList;
	private TextView expValueTv;
	static boolean isCurrentTask = true;
	public static int menuIndex = 0;
	private MenuAdapter mAdapter;
	private CircleImageView headPortrait;
	private RelativeLayout portraitLayout;
	private TextView nickName;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.layout_base_sliding);
		findViews();
		initViews();
		// init360Recommend();
	}

	private void findViews() {
		mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_layout);
		mMenu = (RelativeLayout) findViewById(R.id.menu);
		mBaseTitle = (FrameLayout) findViewById(R.id.base_title_container);
		mBaseContent = (FrameLayout) findViewById(R.id.base_content_container);
		menuList = (ListView) findViewById(R.id.menu_lv);
		headPortrait = (CircleImageView) findViewById(R.id.head_portrait_slide);
		portraitLayout = (RelativeLayout) findViewById(R.id.portrait_layout);
		nickName = (TextView) findViewById(R.id.nick_name);
		expValueTv = (TextView) findViewById(R.id.exp_value);
	}

	private void initViews() {
		mContext = BaseSlideActivity.this;
		resetUserInfo();
		mAdapter = new MenuAdapter(BaseSlideActivity.this);
		menuList.setAdapter(mAdapter);
		menuList.setOnItemClickListener(new MenuOnItemClickListener());
		mSlidingPaneLayout.setSliderFadeColor(Color.TRANSPARENT);
		mSlidingPaneLayout.setShadowResource(R.drawable.slide_menu_shadow);
		portraitLayout.setOnClickListener(new PersonUserInfoListener());
		headPortrait.setOnClickListener(new PersonUserInfoListener());
		nickName.setOnClickListener(new PersonUserInfoListener());
		enableSlide();
	}

	private void givePraise() {
		UpdateManager.shareUpdateExludingMarket(this);
		MobclickAgentData.comment(this);
	}

	@Override
	public void setContentView(View view) {
		if (view != null) {
			mBaseContent.addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		} else {
			super.setContentView(view);
		}
	}

	@Override
	public void setContentView(int layoutId) {
		View view = getLayoutInflater().inflate(layoutId, null);
		setContentView(view);
	}

	public void disableSlide() {
		mMenu.setLayoutParams(new SlidingPaneLayout.LayoutParams(0, 100));
	}

	public void enableSlide() {
		mMenu.setLayoutParams(new SlidingPaneLayout.LayoutParams(2 * Utils.getScreenWidth(this) / 3, LayoutParams.FILL_PARENT));
	}

	protected void openmSlidingPane() {
		mSlidingPaneLayout.openPane();
	}

	protected void closeSlidingPane() {
		mSlidingPaneLayout.closePane();
	}

	protected boolean isSlidingOpening() {
		return mSlidingPaneLayout.isOpen();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mAdapter.notifyDataSetChanged();
		resetUserInfo();
		checkFeedback();

	}

	private void checkFeedback() {
		if (!AppPreference.isHasFeedback(mContext)) {
			Conversation conversation = new FeedbackAgent(mContext).getDefaultConversation();
			conversation.sync(new SyncListener() {

				@Override
				public void onSendUserReply(List<Reply> arg0) {
				}

				@Override
				public void onReceiveDevReply(List<Reply> arg0) {
					if (arg0 != null && arg0.size() > 0) {
						AppPreference.setHasFeedback(mContext, true);
						mAdapter.addRedPoint = true;
						mAdapter.notifyDataSetChanged();
					} else {
						mAdapter.addRedPoint = false;
						mAdapter.notifyDataSetChanged();
					}
				}
			});
		} else {
			mAdapter.addRedPoint = true;
			mAdapter.notifyDataSetChanged();
		}
	}

	private void resetUserInfo() {
		String headPortraitURL = AppPreference.getHeadPortrait(BaseSlideActivity.this);
		if (headPortraitURL != null) {
			Uri imageUri = Uri.parse(headPortraitURL);
			headPortrait.setImageURI(null);
			headPortrait.setImageURI(imageUri);
		} else {
			headPortrait.setImageResource(R.drawable.head_portrait_default);
		}

		String nickNameStr = AppPreference.getNickName(BaseSlideActivity.this);
		if (nickNameStr != null) {
			nickName.setSingleLine(true);
			nickName.setText(StringUtil.subString(nickNameStr, 12));
		} else {
			nickName.setSingleLine(false);
			nickName.setText("点击登录\n可同步记录");
		}

		if (UserInterface.getInstance(mContext).isLogin()) {
			expValueTv.setVisibility(View.VISIBLE);
			expValueTv.setText("经验值 " + AppPreference.getEXP(mContext));
		}
	}

	class PersonUserInfoListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (UserInterface.getInstance(BaseSlideActivity.this).isLogin()) {
				PersonalAccountActivity.startActivity(BaseSlideActivity.this, PersonalAccountActivity.TYPE_OTHER);
			} else {
				RegisterActivity.startActivity(BaseSlideActivity.this, RegisterActivity.TO_PERSON_ACCOUNT);
			}
		}
	}

	class MenuOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

			switch (position) {
			case 0:
				WordsBookActivity.startActivity(BaseSlideActivity.this);
				break;
			case 1:
				SettingPlanActivity.startActivity(BaseSlideActivity.this, StaticData.MODE_UPDATE);
				break;
			case 2:
				PunchOutCalendarActivity.startActivity(BaseSlideActivity.this);
				break;
			case 3:
				if (UserInterface.getInstance(mContext).isLogin()) {
					LeaderboardActivity.startActivity(mContext);
				} else {
					RegisterActivity.startActivity(mContext, RegisterActivity.TO_LEADER_BOARD);
				}
				break;
			case 4:
				SettingActivity.startActivity(BaseSlideActivity.this);
				break;
			case 5:
				UpdateManager.downloadOrStartZhimiListenApp(BaseSlideActivity.this);
				break;
			case 6:
				if (NightModeUtil.getDayNightMode(BaseSlideActivity.this) == NightModeUtil.THEME_SUN) {
					NightModeUtil.setDayNightMode(BaseSlideActivity.this, NightModeUtil.THEME_NIGHT);
				} else {
					NightModeUtil.setDayNightMode(BaseSlideActivity.this, NightModeUtil.THEME_SUN);
				}
				resetTheme();
				openmSlidingPane();
				mAdapter.notifyDataSetChanged();
				break;
			case 7:
				givePraise();

				break;
			case 8:
				new ShareDialog(BaseSlideActivity.this, Utils.shareFrom.slideMenu);

				break;
			}

			// new Thread(new Runnable() {
			// @Override
			// public void run() {
			// try {
			// Thread.sleep(800);
			// } catch (InterruptedException e) {
			// e.printStackTrace();
			// }
			// mSlidingPaneLayout.closePane();
			// }
			// }).start();
			// mSlidingPaneLayout.closePane();

		}
	}

	/**
	 * 隐藏自定义标题
	 */
	public void hideTitle() {
		mBaseTitle.setVisibility(View.GONE);
	}

	public void setTitleView(View view) {
		if (view != null) {
			mBaseTitle.addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		}
	}

	public void setTitleView(int layoutId) {
		View view = getLayoutInflater().inflate(layoutId, null);
		setTitleView(view);
	}

	public int getPosition() {
		return menuIndex;
	}

	protected void immersionBaseStatusBarForXiaoMi() {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, Utils.changeDipToPx(mContext, 70));
		mBaseTitle.setLayoutParams(lp);
	}
}
